# -*- coding: utf-8 -*-
from collections import deque


class Person:

    def __init__(self, name, preferences=[]):
        """Initializes the preferences hashtable.
        O(n)"""
        self.prefnames = deque()
        self.name = name
        self.fiancee = None
        self.prefs = dict()
        i = 0
        for prospectname in preferences.split(","):
            prospectname = prospectname.strip()
            self.prefs[prospectname] = i
            self.prefnames.appendleft(prospectname)
            i = i + 1

    def prefers(self, p1):
        """Determins wheter this person prefers its current
        fiancee or the one specified in the parameter.
        O(1)"""
        result = True
        if self.fiancee is not None:
            result = self.prefs[p1.name] > self.prefs[self.fiancee.name]

        return result

    def engage(self, p):
        self.fiancee = p
        p.fiancee = self

    def break_up(self):
        if self.fiancee is not None:
            self.fiancee.fiancee = None
            self.fiancee = None

    def get_prospect(self):
        if len(self.prefnames) > 0:
            return self.prefnames.pop()
        return None
